﻿using PopupWinTool.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Threading;
using VM.Shared.ReturnValue;
using VM.Shared.TestAttribute;

namespace PopupWinTool
{
    public class 弹窗工具
    {
        [ExposedMethodAttribute("")]
        public bool 信号发生器FM设置(int StationNumb, string 端口名称, string IP地址, int IP端口号, int Pass按钮值, int Fail按钮值, out string 错误信息, out string 返回值, double 调制 = 22.5, double 频率 = 1000, double 幅值 = 30, double 增量 = 5, string 返回值格式 = "调制：{调制}KZ，频率：{频率}MZ，幅值：{幅值}DU")
        {
            错误信息 = "";
            返回值 = "";
            try
            {
                bool? ret = null;
                Window win = null;
                信号发生器SG1501B设置FM vm = null;
                double 设置调制值 = 0d;
                double 设置频率值 = 0d;
                double 设置幅值值 = 0d;
                Application.Current.Dispatcher.Invoke(() =>
                {
                    vm = new 信号发生器SG1501B设置FM()
                    {
                        工站 = StationNumb,
                        端口名称 = 端口名称,
                        调制 = 调制,
                        频率 = 频率,
                        幅值 = 幅值,
                        IP地址 = IP地址,
                        IP端口号 = IP端口号,
                        //Pass按钮值 = Pass按钮值,
                        //Fail按钮值 = Fail按钮值,
                        增量 = 增量,
                    };
                    //根据工站设置Pass、Fail点位
                    switch (StationNumb)
                    {
                        case 1:
                            vm.Pass按钮值 = Pass按钮值;
                            vm.Fail按钮值 = Fail按钮值;
                            break;
                        case 2:
                            vm.Pass按钮值 = Pass按钮值 + 2;
                            vm.Fail按钮值 = Fail按钮值 + 2;
                            break;
                        default:
                            break;
                    }
                    vm.Init();  //初始化界面参数
                    win = new Window()
                    {
                        WindowStyle = WindowStyle.None,
                        ResizeMode = ResizeMode.NoResize,
                        WindowStartupLocation = WindowStartupLocation.CenterScreen,
                        Width = vm.Width,
                        Height = vm.Height,
                    };

                    win.Content = vm;
                    ret = win.ShowDialog();
                    设置调制值 = vm.调制;
                    设置频率值 = vm.频率;
                    设置幅值值 = vm.幅值;
                });

                返回值 = 返回值格式.Replace("{调制}", 设置调制值.ToString())
                                       .Replace("{频率}", 设置频率值.ToString())
                                       .Replace("{幅值}", 设置幅值值.ToString());
                if (ret != null && ret == true)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                错误信息 = ex.Message;
            }
            return false;
        }
        [ExposedMethodAttribute("")]
        public bool 信号发生器FM设置_带幅值输出(int StationNumb, string 端口名称, string IP地址, int IP端口号, int Pass按钮值, int Fail按钮值, out string 信息, out string 返回值,out double 幅值输出, double 调制 = 22.5, double 频率 = 1000, double 幅值 = 30, double 增量 = 5, double 幅值下限 = 30, double 幅值上限 = 100,string 返回值格式 = "调制：{调制}KZ，频率：{频率}MZ，幅值：{幅值}DU")
        {
            信息 = "";
            返回值 = "";
            幅值输出 = 0d;
            try
            {
                bool? ret = null;
                Window win = null;
                信号发生器SG1501B设置FM vm = null;
                double 设置调制值 = 0d;
                double 设置频率值 = 0d;
                double 设置幅值值 = 0d;
                string msg = "";
                Application.Current.Dispatcher.Invoke(() =>
                {
                    vm = new 信号发生器SG1501B设置FM()
                    {
                        工站 = StationNumb,
                        端口名称 = 端口名称,
                        调制 = 调制,
                        频率 = 频率,
                        幅值 = 幅值,
                        IP地址 = IP地址,
                        IP端口号 = IP端口号,
                        //Pass按钮值 = Pass按钮值,
                        //Fail按钮值 = Fail按钮值,
                        增量 = 增量,
                        幅值下限 = 幅值下限,
                        幅值上限 = 幅值上限,
                    };
                    //根据工站设置Pass、Fail点位
                    switch (StationNumb)
                    {
                        case 1:
                            vm.Pass按钮值 = Pass按钮值;
                            vm.Fail按钮值 = Fail按钮值;
                            break;
                        case 2:
                            vm.Pass按钮值 = Pass按钮值 + 2;
                            vm.Fail按钮值 = Fail按钮值 + 2;
                            break;
                        default:
                            break;
                    }
                    vm.Init();  //初始化界面参数
                    win = new Window()
                    {
                        WindowStyle = WindowStyle.None,
                        ResizeMode = ResizeMode.NoResize,
                        WindowStartupLocation = WindowStartupLocation.CenterScreen,
                        Width = vm.Width,
                        Height = vm.Height,
                    };

                    win.Content = vm;
                    ret = win.ShowDialog();
                    设置调制值 = vm.调制;
                    设置频率值 = vm.频率;
                    设置幅值值 = vm.幅值;
                    msg = vm.Message;
                });

                返回值 = 返回值格式.Replace("{调制}", 设置调制值.ToString())
                                   .Replace("{频率}", 设置频率值.ToString())
                                   .Replace("{幅值}", 设置幅值值.ToString());
                幅值输出 = 设置幅值值;
                if (!string.IsNullOrWhiteSpace(msg))
                {
                    信息 = msg;
                }
                if (ret != null && ret == true)
                {
                    return true;
                }
                else
                {
                    if (!string.IsNullOrWhiteSpace(msg))
                    {
                        信息 = msg;
                    }
                    return false;
                }
            }
            catch (Exception ex)
            {
                信息 = ex.Message;
            }
            return false;
        }

        //================================ 本地测试使用的方法 ===========================
        /// <summary>
        /// 应用程序中调用的方法
        /// </summary>
        /// <param name="返回结果"></param>
        /// <returns></returns>
        //[ExposedMethodAttribute("")]
        public bool 弹出测试窗体(out string 返回结果)
        {
            返回结果 = "";
            bool? ret = null;
            Window win = null;
            测试窗体 vm = null;
            string ss = "";
            //加上 Application.Current.Dispatcher 之后，在控制台项目中就不能调用了
            Application.Current.Dispatcher.Invoke(() =>
            {
                vm = new 测试窗体();
                win = new Window()
                {
                    WindowStyle = WindowStyle.None,
                    ResizeMode = ResizeMode.NoResize,
                    WindowStartupLocation = WindowStartupLocation.CenterScreen,
                    Width = vm.Width,
                    Height = vm.Height,
                };


                win.Content = vm;
                ret = win.ShowDialog();
                if (vm.Tag != null && !string.IsNullOrWhiteSpace(vm.ToString()))
                {
                    ss = vm.Tag.ToString();
                }
            });

            if (!string.IsNullOrWhiteSpace(ss))
            {
                返回结果 = ss;
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 控制台应用中调用的方法
        /// </summary>
        /// <param name="返回结果"></param>
        /// <returns></returns>
        public bool 弹出测试窗体2(out string 返回结果)
        {

            返回结果 = "";
            bool? ret = null;
            UserControl uc = null;
            测试窗体 vm = new 测试窗体();
            Window win = new Window()
            {
                WindowStyle = WindowStyle.None,
                WindowStartupLocation = WindowStartupLocation.CenterScreen,
                Width = vm.Width + 20,
                Height = vm.Height + 10,
            };

            win.Content = vm;
            ret = win.ShowDialog();
            uc = vm;

            if (uc.Tag != null && !string.IsNullOrWhiteSpace(uc.ToString()))
            {
                返回结果 = uc.Tag.ToString();
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
